package org.dubnation.org.controller;

import org.dubnation.base.util.audit.ErrorInfo;
import org.dubnation.org.domain.Shop;
import org.dubnation.org.mapper.EmployeeMapper;
import org.dubnation.org.service.IEmployeeService;
import org.dubnation.org.domain.Employee;
import org.dubnation.org.query.EmployeeQuery;
import org.dubnation.base.util.AjaxResult;
import org.dubnation.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param employee  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult insertOrUpdate(@RequestBody Employee employee){
        if( employee.getId()!=null)
            employeeService.update(employee);
        else
            employeeService.insert(employee);
        return AjaxResult.me();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id")Long id) {
        Employee employee = employeeService.queryById(id);
        return AjaxResult.me().setData(employee);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult queryAll(){
        List< Employee> list = employeeService.queryAll();
        return AjaxResult.me().setData(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody EmployeeQuery query) {
        PageInfo<Employee> pageInfo = employeeService.queryPage(query);
        return AjaxResult.me().setData(pageInfo);
    }

    /**
    * 批量删除数据
    * @param ids
    * @return
*/
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        employeeService.batchDelete(ids);
        return AjaxResult.me();
    }

    /**
     * 店铺审核驳回
     * @param errorInfo
     * @return
     */
    @PostMapping("/reject")
    public AjaxResult shopAuditReject(@RequestBody ErrorInfo errorInfo) throws MessagingException {
        employeeService.shopAuditReject(errorInfo);
        return AjaxResult.me().setMessage("已向商家发送驳回修改邮件");
    }

    /**
     * 店铺审核通过
     * @param shop
     * @return
     * @throws MessagingException
     */
    @PutMapping("/pass")
    public AjaxResult shopAuditPass(@RequestBody Shop shop){
        try {
            employeeService.shopAuditPass(shop);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return AjaxResult.me().setMessage("已向商家发送激活邮件");
    }
}
